package com.tuanzi.loan.web.bean.family;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.system.Dictionary;
import com.tuanzi.loan.business.service.family.LoanApiOrderService;
import com.tuanzi.loan.vo.request.family.LoanApiOrderSearchRequest;
import com.tuanzi.loan.vo.response.family.LoanApiOrderSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.family.LoanApiOrderLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import java.util.List;
import java.util.Objects;

import static com.tuanzi.loan.business.constants.DictionaryCode.PRODUCT_MONEY;
import static com.tuanzi.loan.business.constants.DictionaryCode.WHOLEPROCESSOWNER;

/**
 * 查询订单列表
 * @author 2018/01/12
 *
 */
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanApiOrderBean extends JSFListingBean<LoanApiOrderSearchRequest, LoanApiOrderSearchResponse> {
    
	private static final long serialVersionUID = -303671670177027647L;
	
	@Autowired
	private LoanApiOrderService loanApiOrderService;
	
	@PostConstruct
	private void init() {
		initLazyDataModel();
	}
	
	public void onReset() {
		initLazyDataModel();
	}

	public String onEdit(String id) {
		return redirect("/views/family/loanapiorder/info.xhtml?id=" + id);
	}

	private void initLazyDataModel() {
		$ = new LoanApiOrderSearchRequest();
		if (Objects.nonNull($.getType())){
            $.setType(getType(String.valueOf($.getType())));
        }
		lazyDataModel = new LoanApiOrderLazyDataModel($);
	}

	public String onOrderDetail(String id){
		return redirect("/views/family/loanapiorder/orderdetail-list.xhtml?id=" + id);
	}
    private Integer getType(String type){
        for (Dictionary dictionary:dictionaryService.findAllByCode(WHOLEPROCESSOWNER)) {
            if (dictionary.getValue1().equals(type)){
                return Integer.parseInt(dictionary.getName());
            }else{
                continue;
            }
        }
        return 0;
    }
    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(WHOLEPROCESSOWNER);
    }

    public String translateType(Integer type){
	    return dictionaryService.findValueByCodeAndName(WHOLEPROCESSOWNER,String.valueOf(type));
    }
}
